package threadcoreknowledge.createthread.threadobjectclasscommommethods;

public class WaitNotifyPrintOddEveWait {

    private static int count;
    private static volatile Object lock = new Object();

    public static void main(String[] args) {
        new Thread(new TurningRunner(),"偶数").start();
        new Thread(new TurningRunner(),"奇数").start();
    }


    static class TurningRunner implements  Runnable{

        @Override
        public void run() {
            synchronized (lock){
                while (count <= 100){
                    //一拿到锁，只要条件符合就打印
                    System.out.println(Thread.currentThread().getName()+ " : "+count++);
                    lock.notify();
                    //打印完之后，唤醒其他线程，然后让自己这个线程休眠
                    if (count <= 100){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    }

}
